//
//  BrowserBonjour.m
//  MiCO
//
//  Created by zfw on 15/6/5.
//  Copyright (c) 2015年 mxchip. All rights reserved.
//

#import "BrowserBonjour.h"

@interface BrowserBonjour ()
{
    int g_count;
}
@property(strong,nonatomic)NSNetServiceBrowser   *m_netServiceBrowser;
@property(strong,nonatomic)NSMutableArray        *m_services;

//循环5秒查数据
@property(strong,nonatomic)NSTimer                 *m_loopTimer;
@property(strong,nonatomic)NSString                *m_serviceType;
@property(strong,nonatomic)NSString                *m_domain;
@end

@implementation BrowserBonjour
@synthesize m_netServiceBrowser,m_services;
@synthesize m_loopTimer,m_domain,m_serviceType;

//重写init，加入一点初始化条件
- (instancetype)init;
{
    if(self=[super init])
    {
        m_services=[[NSMutableArray alloc]init];
        g_count=0;
    }
    return self;
}


- (void)getMdns:(NSString*)serviceType  andDomain:(NSString*)domain
{
    //init
    m_serviceType = serviceType;
    m_domain = domain;
    g_count = 0;
    m_services=nil;
    m_services=[[NSMutableArray alloc]init];
    self.m_netServiceBrowser = nil;
    self.m_netServiceBrowser = [[NSNetServiceBrowser alloc] init];
    self.m_netServiceBrowser.delegate=self;
    
    [self.m_netServiceBrowser searchForServicesOfType:serviceType inDomain:domain];//此方法不会阻塞
}

-(void)stopSearchObject
{
    if(m_loopTimer)
    {
        [m_loopTimer invalidate];
        m_loopTimer=nil;
    }
    [self.m_netServiceBrowser stop];
}
-(void)stopMdns
{
    if(m_loopTimer)
    {
        [m_loopTimer invalidate];
        m_loopTimer=nil;
    }
    [self.m_netServiceBrowser stop];
    self.m_netServiceBrowser=nil;
    m_services=nil;
}
- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didNotSearch:(NSDictionary *)errorDict
{
   // NSLog(@"error");
}
// New service was found
- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)service moreComing:(BOOL)moreComing;
{
    //NSLog(@"didFindService");
    g_count++;
    NSMutableDictionary *moduleService=[[NSMutableDictionary alloc]init];
    service.delegate=self;
   // NSLog(@"count=%ld", service.addresses.count);
    [moduleService setObject:[service name] forKey:@"Name"];
    [moduleService setObject:service forKey:@"BonjourService"];
    [moduleService setObject:@YES forKey:@"resolving"];
    [service startMonitoring];
    
    [m_services addObject:moduleService];
    [service resolveWithTimeout:5.0];
    
    // If more entries are coming, no need to update UI just yet
    if ( !moreComing ) {
        //[self.m_netServiceBrowser stop];
        //self.m_netServiceBrowser=nil;
    }
}

#pragma mark - NSNetServiceDelegate

- (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
{
    //NSLog(@"didNotResolve");
}

- (void)netServiceDidStop:(NSNetService *)sender
{
    //NSLog(@"netServiceDidStop");
}


- (void)netServiceDidResolveAddress:(NSNetService *)service
{
    //NSLog(@"netServiceDidResolveAddress");
    [service stop];//这行很重要
    g_count--;
    if(g_count==0)
    {
        //[self killTimer];
        [self.delegate returnMndsData:m_services];
        if(m_loopTimer)
        {
            [m_loopTimer invalidate];
            m_loopTimer=nil;
        }
        
        m_loopTimer=[NSTimer scheduledTimerWithTimeInterval:LOOPTIMER target:self selector:@selector(continueSearch) userInfo:nil repeats:NO];
    }
    /*
     {
     "Firmware Rev" = <4d4b3332 38385f31 5f305f35>;
     "Hardware Rev" = <4d4b3332 38385f31>;
     MAC = <43383a39 333a3436 3a39313a 38313a38 35>;
     "MICO OS Rev" = <31303838 30303032 2e303331>;
     Manufacturer = <4d584348 49502049 6e632e>;
     Model = <4d69434f 4b69742d 33323838>;
     Protocol = <636f6d2e 6d786368 69702e6d 69636f6b 6974>;
     Seed = <333135>;
     binding＝1;//后来增加一个绑定
     }
     */
 }

-(void)continueSearch
{
    [self stopSearchObject];
    [self getMdns:m_serviceType andDomain:m_domain];
}

- (void)netService:(NSNetService *)service didUpdateTXTRecordData:(NSData *)data
{
    //NSLog(@"didUpdateTXTRecordData");
}

@end
